package com.it.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.domain.InventoryTransfer;
import com.it.domain.ResponseResult;
import com.it.mapper.InventoryTransferMapper;
import com.it.service.IInventoryTransferService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;

/**
 * 库存调拨Controller
 */
@RestController
@RequestMapping("/transfer/transfer")
public class InventoryTransferController {
@Autowired
private IInventoryTransferService iInventoryTransferService;
    @Autowired
    private InventoryTransferMapper inventoryTransferMapper;

    /**
     * 分页查询库存调拨列表
     */
    @GetMapping("/list")
    public ResponseResult<IPage<InventoryTransfer>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long transferId,
            @RequestParam(required = false) Long sourceInventoryId,
            @RequestParam(required = false) Long destinationInventoryId,
            @RequestParam(required = false) Integer quantity,
            @RequestParam(required = false) String transferDate,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {

        // 构建分页对象
        Page<InventoryTransfer> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<InventoryTransfer> queryWrapper = Wrappers.lambdaQuery();

        if (transferId != null) {
            queryWrapper.eq(InventoryTransfer::getTransferId, transferId);
        }

        if (sourceInventoryId != null) {
            queryWrapper.eq(InventoryTransfer::getSourceInventoryId, sourceInventoryId);
        }

        if (destinationInventoryId != null) {
            queryWrapper.eq(InventoryTransfer::getDestinationInventoryId, destinationInventoryId);
        }

        if (quantity != null) {
            queryWrapper.eq(InventoryTransfer::getQuantity, quantity);
        }

        if (transferDate != null) {
            queryWrapper.eq(InventoryTransfer::getTransferDate, LocalDate.parse(transferDate));
        }

        // 日期范围查询
        if (startDate != null && endDate != null) {
            queryWrapper.between(InventoryTransfer::getTransferDate,
                    LocalDate.parse(startDate),
                    LocalDate.parse(endDate));
        }

        // 默认按调拨ID降序
        queryWrapper.orderByDesc(InventoryTransfer::getTransferId);

        // 执行查询
        IPage<InventoryTransfer> result = iInventoryTransferService.page(page, queryWrapper);

        return ResponseResult.success(result);
    }

    /**
     * 获取库存调拨详细信息
     */
    @GetMapping("/{transferId}")
    public ResponseResult<InventoryTransfer> getInfo(@PathVariable Long transferId) {
        InventoryTransfer transfer = inventoryTransferMapper.selectById(transferId);
        return transfer != null ?
                ResponseResult.success(transfer) :
                ResponseResult.error("调拨记录不存在");
    }

    /**
     * 新增库存调拨
     */
    @PostMapping
    public ResponseResult<String> add(@RequestBody InventoryTransfer inventoryTransfer) {
        // 简单验证
        if (inventoryTransfer.getSourceInventoryId() == null ||
                inventoryTransfer.getDestinationInventoryId() == null) {
            return ResponseResult.error("源库存和目标库存不能为空");
        }

        if (inventoryTransfer.getQuantity() == null || inventoryTransfer.getQuantity() <= 0) {
            return ResponseResult.error("调拨数量必须大于0");
        }

        // 设置默认调拨日期
        if (inventoryTransfer.getTransferDate() == null) {
            inventoryTransfer.setTransferDate(LocalDateTime.now());
        }

        int result = inventoryTransferMapper.insert(inventoryTransfer);
        return result > 0 ?
                ResponseResult.success("新增成功") :
                ResponseResult.error("新增失败");
    }

    /**
     * 修改库存调拨
     */
    @PutMapping
    public ResponseResult<String> edit(@RequestBody InventoryTransfer inventoryTransfer) {
        if (inventoryTransfer.getTransferId() == null) {
            return ResponseResult.error("调拨ID不能为空");
        }

        int result = inventoryTransferMapper.updateById(inventoryTransfer);
        return result > 0 ?
                ResponseResult.success("修改成功") :
                ResponseResult.error("修改失败");
    }

    /**
     * 删除库存调拨
     */
    @DeleteMapping("/{transferIds}")
    public ResponseResult<String> remove(@PathVariable Long[] transferIds) {
        if (transferIds == null || transferIds.length == 0) {
            return ResponseResult.error("请选择要删除的记录");
        }

        int result = inventoryTransferMapper.deleteBatchIds(Arrays.asList(transferIds));
        return result > 0 ?
                ResponseResult.success("删除成功") :
                ResponseResult.error("删除失败");
    }
}